package com.zxcl.listener;

import com.alibaba.fastjson2.JSON;
import com.zxcl.config.WxOfficialProperties;
import com.zxcl.constant.WxConstant;
import com.zxcl.model.WxOfficialModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@RocketMQMessageListener(topic = WxConstant.TX_WX_TOPIC, consumerGroup = WxConstant.TX_WX_CONSUMER_GROUP,namespace = WxConstant.MQ_NAME_SPACE)
public class WxOfficialListener implements RocketMQListener<String> {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private WxOfficialProperties wxOfficialProperties;

    @Override
    public void onMessage(String message) {
        System.out.println(message);
        WxOfficialModel wxOfficialModel = JSON.parseObject(message, WxOfficialModel.class);
        // 先处理地址
        String wxToken = redisTemplate.opsForValue().get(WxConstant.WX_OFFICIAL_TOKEN_KEY);
        System.out.println(wxToken);
        String realMsgUrl = String.format(wxOfficialProperties.getMsgUrl(), wxToken);
        System.out.println(realMsgUrl);
        String result = new RestTemplate().postForObject(realMsgUrl, wxOfficialModel, String.class);
        System.out.println(result);
    }
}
